package com.login.service;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.transaction.Transactional;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.entity.Ope;
import com.interfaces.OpeDao;
import com.interfaces.PowerDao;
import com.interfaces.OpePowerDao;
import com.interfaces.SysdicDao;
import com.login.entity.JsonLogin;
import com.login.entity.LoginPowerMenu;
import com.setting.service.SysdicService;
import com.utils.CglibBeanCopierUtils;
import com.utils.JsonResult;
import com.utils.Utils;

/**
 * @author 孙梦楠
 * @date 2017年7月12日 下午6:39:55
 * @version V1.0.0
 */
@Service
public class LoginService {

	@Autowired
	private OpeDao opedao;
	@Autowired
	private PowerDao powerdao;
	@Autowired
	private OpePowerDao opepowerdao;

	@Autowired
	private SysdicService sysdicservice;
	
	@Autowired
	private SysdicDao sysdicdao;

	private static final Logger log = LoggerFactory.getLogger(LoginService.class);

	// 增加用户（test）
	@Transactional
	public JsonResult<Ope> addope(String opecode, String opepassword) {
		// 检查入口参数（第一件事，非常重要，任何方法有参数传入都要进行合理性检查）

		Ope ope = new Ope();

		if (Utils.notNullAndEmpty(opecode)) {// trim()去掉两端空字符串；isEmpty()是否为空
			if (Utils.notNullAndEmpty(opepassword)) {

				ope.setOpecode(opecode.trim());
				ope.setOpepassword(opepassword.trim());
				opedao.save(ope);
				return new JsonResult<Ope>(1, "创建用户成功", ope);

			}
			return new JsonResult<Ope>(2, "创建用户失败..密码为空", null);

		}
		return new JsonResult<Ope>(3, "创建用户失败..账号为空", null);

	}

	// 登录检查
	@Transactional
	public JsonResult<Ope> logincheck(String opecodecookie, String opepasswordcookie, HttpServletRequest request) {

		if (opecodecookie != null && !(opecodecookie.equals("null")) && opepasswordcookie != null
				&& !(opepasswordcookie.equals("null"))) {

			// 判断cookie的账号密码是否和数据库对应

			Ope opehasfind = opedao.findByOpecode(opecodecookie);// 找到指定对象
			if (opehasfind != null) {

				// 查看密码是否对应
				if (opehasfind.getOpepassword().equals(opepasswordcookie)) {

					if (request.getSession().getAttribute("opehaslogin") != null) {

						return new JsonResult<Ope>(9, "短信验证成功..进入主页面", null);

					}
					return new JsonResult<Ope>(4, "登录成功..进行短信验证", opehasfind);
				}
				return new JsonResult<Ope>(8, "未登录..跳转到登录界面", null);
			}
			return new JsonResult<Ope>(8, "未登录..跳转到登录界面", null);
		}
		return new JsonResult<Ope>(8, "未登录..跳转到登录界面", null);
	}

	// 登录
	@Transactional
	public JsonResult<JsonLogin> login(String opecode, String opepassword, String opeverification, String language,
			HttpServletResponse response, HttpServletRequest request) {

		// 得到的账号密码不为空
		if (!(opecode == null || opecode.trim().isEmpty()) && !(opepassword == null || opepassword.trim().isEmpty())
				&& !(opeverification == null || opeverification.trim().isEmpty())) {

			Ope opehasfind = opedao.findByOpecode(opecode);

			if (opehasfind != null) {
				if (opehasfind.getOpeverification().equals(opeverification)) {

					if (opehasfind.getOpepassword().equals(opepassword)) {

						JsonLogin jsonLogin = new JsonLogin();
						// 设置cookie账号
						Cookie opecodecookie = new Cookie("opecodecookie", opehasfind.getOpecode());
						opecodecookie.setPath("/");
						response.addCookie(opecodecookie);
						// 设置cookie密码
						Cookie opepasswordcookie = new Cookie("opepasswordcookie", opehasfind.getOpepassword());
						opepasswordcookie.setPath("/");
						response.addCookie(opepasswordcookie);
						// 设置cookie组织架构
						Cookie opeorgcookie = new Cookie("opeorgcookie", opehasfind.getOpeorg().toString());
						opepasswordcookie.setPath("/");
						response.addCookie(opeorgcookie);
						// 设置cookie操作员类别
						Cookie opetypecookie = new Cookie("opetypecookie", opehasfind.getOpetype());
						opepasswordcookie.setPath("/");
						response.addCookie(opetypecookie);
						// 设置语言
						Cookie languagecookie = new Cookie("languagecookie", language);
						opepasswordcookie.setPath("/");
						response.addCookie(languagecookie);

						log.info(request.getHeader("Cookie"));
						log.info("输入" + opecode);
						log.info("输入" + opepassword);
						log.info(opehasfind.getOpecode());
						log.info(opehasfind.getOpepassword());
						log.info(opehasfind.getOpeorg().toString());
						log.info(opehasfind.getOpetype());
						log.info(language);

//						int len = opehasfind.getOpemenu().length();
//						String[][] menu = new String[len][2];
//						char[] arr = new char[len];
//						for (int i = 0; i < len; i++) {
//							arr[i] = opehasfind.getOpemenu().charAt(i);
//						}
//						int i = 0;
//						for (char c : arr) {
//							menu[i][0] = sysdicservice.finddisplay("0030", c + "", language);
//							menu[i][1] = sysdicdao.findBySysdictypeAndSysdicvalue("0030", c + "").getSysdicremake();
//							i++;
//						}
						CglibBeanCopierUtils.copyProperties(opehasfind, jsonLogin);
						
						jsonLogin.setMenuname(sysdicservice.finddisplay("0030", opehasfind.getOpemenu(), language));
			
						log.info("LoginSuccess");
						return new JsonResult<JsonLogin>(4, "登录成功", jsonLogin);

					}
					log.info("LoginFail..ErrorPswd");
					return new JsonResult<JsonLogin>(5, "登录失败..密码错误", null);

				}
				return new JsonResult<JsonLogin>(6, "登录失败..短信验证码错误", null);

			}
			log.info("LoginFail..ErrorUser");
			return new JsonResult<JsonLogin>(6, "登录失败..用户不存在", null);
		}
		return new JsonResult<JsonLogin>(7, "登录失败..输入有误", null);

	}
	

	

}
